<template src="./index.html"></template>

<script>
import MedicalRecordMarriageService from "../../../../../_services/medicalRecordMarriage.service";
export default {
  name: "TianDingPatientMeMarriageHistoryComponent",
  props: ["user"],
  data() {
    return {
      params: {
        MarriageID: "0",
        UserID: "0",
        MarriageableAge: "10",
        ContraceptionWay: "",
        IsAciesis: "0",
        ChildrenNumber: "0",
        ChildrenNumber1: "0",
        ContraceptionTime: "1",
        AciesisTime: "1",
        Remark: "",
        FullTermNumber: 0,
        PrematureNumber: 0,
        AbortionNumber: 0
      },
      showContraceptionWay: false,
      ContraceptionWayList: [
        {
          key: 0,
          value: "未避孕",
          isRadio: true,
          check: true,
          show: true
        },
        {
          key: 1,
          value: "避孕药",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 2,
          value: "上环",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 3,
          value: "避孕套",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 4,
          value: "体外射精",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 5,
          value: "杀精剂",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 6,
          value: "安全期",
          isRadio: false,
          check: false,
          show: true
        }
      ],
      showMarriageableAge: false,
      MarriageableAgeList: [],
      showContraceptionTime: false,
      ContraceptionTimeList: [],
      showAciesisTime: false,
      AciesisTimeList: []
    };
  },
  mounted() {
    this.utils$.getNumberLabel(this.MarriageableAgeList, 10, 20, "年");
    this.utils$.getNumberLabel(this.ContraceptionTimeList, 1, 10, "年");
    this.utils$.getNumberLabel(this.AciesisTimeList, 1, 10, "年");

    this.getMarriageListByUserID();
  },
  methods: {
    getMarriageListByUserID() {
      this.indicator$.open("正在加载...");
      let user = JSON.parse(this.local$.getItem("tdUser"));
      let params = {};
      params.userid = user.UserID;
      MedicalRecordMarriageService.getMarriageListByUserID(params).then(
        response => {
          console.log(response);
          if (response && response.Data) {
            this.params = response.Data;
            this.utils$.dataFormat(this.params);
            this.params.AciesisTime = this.params.AciesisTime.toString();
            this.params.ChildrenNumber = this.params.ChildrenNumber.toString();
            this.params.ChildrenNumber1 = this.params.ChildrenNumber1.toString();
            this.params.ContraceptionTime = this.params.ContraceptionTime.toString();
            this.params.IsAciesis = this.params.IsAciesis.toString();
            this.params.MarriageableAge = this.params.MarriageableAge.toString();
          }
          console.log(this.params);
          this.indicator$.close();
        }
      );
    },
    getMarriageableAge(event) {
      this.showMarriageableAge = false;
    },
    getContraceptionTime(event) {
      this.showContraceptionTime = false;
    },
    changeContraceptionWay(event) {
      if (this.showContraceptionWay) {
        this.showContraceptionWay = false;
      } else if (!this.showContraceptionWay) {
        this.showContraceptionWay = true;
      }
    },
    getAciesisTime(event) {
      this.showAciesisTime = false;
    },
    getContraceptionWay(event) {
      let temp = [];
      if (event[0] === 0) {
        this.showContraceptionWay = false;
        let item = this.ContraceptionWayList.find(i => i.key === event[0]);
        temp.push(item.value);
        this.params.ContraceptionWay = temp.toString();
      } else {
        event.forEach(key => {
          let item = this.ContraceptionWayList.find(i => i.key === key);
          temp.push(item.value);
        });
        this.params.ContraceptionWay = temp.toString();
      }
    },
    btnPrev() {
      let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
      menu.prev = "prev";
      this.$emit("change", menu);
    },
    btnPrev() {
      let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
      menu.prev = "prev";
      this.$emit("change", menu);
    },
    btnSubmit(next) {
      if (!this.params.ContraceptionWay) {
        this.toast$("请选择避孕方式");
        return;
      }

      this.indicator$.open("正在保存...");

      let user = JSON.parse(this.local$.getItem("tdUser"));

      let params = {
        MarriageID: parseInt(this.params.MarriageID),
        UserID: user.UserID,
        MarriageableAge: parseInt(this.params.MarriageableAge),
        ContraceptionWay: this.params.ContraceptionWay,
        IsAciesis: parseInt(this.params.IsAciesis),
        ChildrenNumber: parseInt(this.params.ChildrenNumber),
        ChildrenNumber1: parseInt(this.params.ChildrenNumber1),
        ContraceptionTime: parseInt(this.params.ContraceptionTime),
        AciesisTime: parseInt(this.params.AciesisTime),
        Remark: this.params.Remark
      };

      if (params.MarriageID > 0) {
        MedicalRecordMarriageService.updateMarriage(params).then(response => {
          this.result(response, next);
        });
      } else {
        MedicalRecordMarriageService.addMarriage(params).then(response => {
          this.result(response, next);
        });
      }
    },
    result(response, next) {
      if (response && response.Data > 0) {
        this.toast$("保存成功！");
        if (next !== undefined) {
          let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
          menu.next = "next";
          this.$emit("change", menu);
        }
      } else {
        this.toast$("保存失败！");
      }
      this.getMarriageListByUserID();
    }
  }
};
</script>
